package cn.keyvalue.ccf.common.util;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;

/**
 * 分词工具，将分类，品牌等数据切分为词语
 * 
 * @author wangxinning
 * 
 */
public class DictUtils {

	/**
	 * 切分关键词
	 * 尽量多的切分，便于搜索，用于分类和品牌字符串
	 * @return
	 */
	public static String[] separateTags( String srcString )
	{
		String[] retArray = null;
		
		//1.原字符串保存
		retArray = (String[])ArrayUtils.add(retArray, srcString);
		
		//2.包含/的
		String sChar = "/";
		if( StringUtils.contains(srcString, sChar) )
		{
			//删除/
			retArray = (String[]) ArrayUtils.add(retArray,
					StringUtils.remove(srcString, sChar));
			///切分
			retArray = (String[]) ArrayUtils.addAll(retArray,
					srcString.split(sChar));
		}
		
		//3.包含空格的
		sChar = " ";
		if( StringUtils.contains(srcString, sChar) )
		{
			//删除/
			retArray = (String[]) ArrayUtils.add(retArray,
					StringUtils.remove(srcString, sChar));
			///切分
			retArray = (String[]) ArrayUtils.addAll(retArray,
					srcString.split(sChar));
		}
		
		//3.包含、的
		sChar = "、";
		if( StringUtils.contains(srcString, sChar) )
		{
			//删除/
			retArray = (String[]) ArrayUtils.add(retArray,
					StringUtils.remove(srcString, sChar));
			///切分
			retArray = (String[]) ArrayUtils.addAll(retArray,
					srcString.split(sChar));
		}
		
		
		
		// 3.包含(的
		sChar = "(";
		if (StringUtils.contains(srcString, "(")
				&& !StringUtils.isNumeric(StringUtils.substringBetween(
						srcString, "(", ")"))) {
			retArray = (String[]) ArrayUtils.add(retArray,
					StringUtils.substringBefore(srcString, sChar));

			retArray = (String[]) ArrayUtils.add(retArray,
					StringUtils.substringBetween(srcString, "(", ")"));
			
			retArray = (String[]) ArrayUtils.add(retArray,
					StringUtils.substringBetween(srcString, "(", "）"));
		}
		
		// 4.包含（的
		sChar = "（";
		if (StringUtils.contains(srcString, "（")
				&& !StringUtils.isNumeric(StringUtils.substringBetween(
						srcString, "（", "）"))) {
			retArray = (String[]) ArrayUtils.add(retArray,
					StringUtils.substringBefore(srcString, sChar));

			retArray = (String[]) ArrayUtils.add(retArray,
					StringUtils.substringBetween(srcString, "（", "）"));
			
			retArray = (String[]) ArrayUtils.add(retArray,
					StringUtils.substringBetween(srcString, "（", ")"));
		}
		 
		//5.中英文分离
		String chineseStr = groupChinese(srcString);
		if( !StringUtils.equals(chineseStr, srcString) )
		{
			retArray = (String[]) ArrayUtils.add(retArray,chineseStr);			
			retArray = (String[]) ArrayUtils.add(retArray,StringUtils.remove(srcString, chineseStr));
		}
		
		//6.过滤重复的
		String[] ret = null;
		for( String s:retArray )
		{
			String tempS = StringUtils.trim(s);
			tempS = StringUtils.remove(tempS, "/");
			tempS = StringUtils.remove(tempS, "()");
			tempS = StringUtils.remove(tempS, "（）");
			
			
			if( StringUtils.isBlank(tempS) )
			{
				continue;
			}
			
			if( ArrayUtils.contains(ret, tempS) )
			{
				continue;
			}
			
			if( StringUtils.equals(tempS, "&") )
			{
				continue;
			}
			
			if( StringUtils.equals(tempS, "﹠") )
			{
				continue;
			}
						
			if( StringUtils.contains(tempS, "(") && !StringUtils.contains(tempS, ")") )
			{
				continue;
			}
			
			if( StringUtils.contains(tempS, "（") && !StringUtils.contains(tempS, "）") )
			{
				continue;
			}
			
			if( !StringUtils.contains(tempS, "(") && StringUtils.contains(tempS, ")") )
			{
				continue;
			}
			
			if( !StringUtils.contains(tempS, "（") && StringUtils.contains(tempS, "）") )
			{
				continue;
			}
					
			ret = (String[])ArrayUtils.add(ret, StringUtils.trim(tempS));
		}
		
		return ret;
	}
	
	
	
	public static void main(String[] args) throws IOException {

		String path = "D:\\PrivateFiles\\Desktop\\q3c_shop_brand_tags.csv";

		File file = new File(path);

		List<String> fList = FileUtils.readLines(file, "utf-8");

		long start = System.currentTimeMillis();
		int i = 0;
		for (String line : fList) {
			
			String tempLine = "," + StringUtils.substringBetween(line, "\"[", "]\"") + ",";
			String[] dictArray = StringUtils.split(tempLine,",");
			
			if(ArrayUtils.contains(dictArray, "中国"))
			{
				System.out.println( tempLine );
			}
		}
		long end = System.currentTimeMillis();
		
		System.out.println( fList.size()  );
		System.out.println( (end - start) + "ms!" );
	}

	public static String groupChinese(String str) {
		// String s = "asdf你好哈哈哦哦qwer";
		// Pattern p = Pattern.compile("[\u4E00-\u9FA5]") ;
		// U+4e00 ~ U+9FB0		
		
		Pattern p1 = Pattern.compile("[\u4E00-\u9FB0]");
		Matcher m = p1.matcher(str);
		String retStr = "";

		int times = 0;
		String startStr = "";
		String endStr = "";
		while (m.find()) {
			String tempStr = m.group();

			if (times == 0) {
				startStr = tempStr;
			} else {
				endStr = tempStr;
			}

			times++;
		}

		if (StringUtils.isNotEmpty(startStr) && StringUtils.isEmpty(endStr)) {
			retStr = startStr;
		} else if (StringUtils.isNotEmpty(startStr)
				&& StringUtils.isNotEmpty(endStr)) {
			retStr = startStr
					+ StringUtils.substringBetween(str, startStr, endStr)
					+ endStr;
		} else {
			retStr = "";
		}

		return retStr;
	}
}

// if( StringUtils.contains(line, "(") &&
// !StringUtils.isNumeric(StringUtils.substringBetween(line, "(",")")) )
// {
// i++;
// System.out.println( i + "  " + line );
// }

// if( StringUtils.contains(line, "（") &&
// !StringUtils.isNumeric(StringUtils.substringBetween(line, "（","）")) )
// {
// i++;
// System.out.println( i + "  " + line );
// }